<?php

class Model_admin extends CI_Model {

	public function __construct()
    {
        $this->load->database();
    }
	
	function validate($username, $password)
	{
		$this->db->where('username', $username);
		$this->db->where('password', $password);
		$query = $this->db->get('admin');
		
		if($query->num_rows == 1)
		{
			return true;
		}		
	}

	function get_db_session_data()
	{
		$query = $this->db->select('user_data')->get('ci_sessions');
		$user = array(); /* array to store the user data we fetch */
		foreach ($query->result() as $row)
		{
		    $udata = unserialize($row->user_data);
		    /* put data in array using username as key */
		    $user['username'] = $udata['username']; 
		    $user['is_logged_in'] = $udata['is_logged_in']; 
		}
		return $user;
	}
	
	function count_jenis_test($search_string=null, $order=null)
    {
		$this->db->select('*');
		$this->db->from('jenis_test');
		if($search_string){
			$this->db->like('name', $search_string);
		}
		if($order){
			$this->db->order_by($order, 'Asc');
		}else{
		    $this->db->order_by('id', 'Asc');
		}
		$query = $this->db->get();
		return $query->num_rows();        
    }
	
	public function get_jenis_test($search_string=null, $order=null, $order_type='Asc', $limit_start=null, $limit_end=null)
    {
	    
		$this->db->select('*');
		$this->db->from('jenis_test');

		if($search_string){
			$this->db->like('name', $search_string);
		}
		$this->db->group_by('id');

		if($order){
			$this->db->order_by($order, $order_type);
		}else{
		    $this->db->order_by('id', $order_type);
		}

        if($limit_start && $limit_end){
          $this->db->limit($limit_start, $limit_end);	
        }

        if($limit_start != null){
          $this->db->limit($limit_start, $limit_end);    
        }
        
		$query = $this->db->get();
		
		return $query->result_array(); 	
    }
	
	function simpan_jenis_test($data)
    {
		$insert = $this->db->insert('jenis_test', $data);
	    return $insert;
	}
	
	function ubah_jenis_test($id, $data)
    {
		$this->db->where('id', $id);
		$this->db->update('jenis_test', $data);
		$report = array();
		$report['error'] = $this->db->_error_number();
		$report['message'] = $this->db->_error_message();
		if($report !== 0){
			return true;
		}else{
			return false;
		}
	}
	
	function delete_jenis_test($id){
		$this->db->where('id', $id);
		$this->db->delete('jenis_test'); 
	}
	
	public function check_user($id_user)
	{
		$this->db->select('*');
		$this->db->from('jawaban_user');
		$this->db->where('id_user', $id_user);
		$query = $this->db->get();
		return $query->num_rows();
	}
	
	public function get_user()
	{
		$this->db->select('*');
		$this->db->from('user');
		$query = $this->db->get();
		return $query->result_array();
	}
	
}

